package com.hxzy.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.controller.admin.employee.req.EmployeeReq;
import com.hxzy.controller.admin.login.req.AdminLoginReq;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.entity.Employee;
import com.hxzy.service.EmployeeService;
import com.hxzy.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【employee(后台管理员表)】的数据库操作Service实现
* @createDate 2023-06-28 16:38:44
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{


    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageInfo<Employee> search(EmployeeReq req) {
        return PageHelper.startPage(req.getPage(),req.getSize())
                .doSelectPageInfo(()-> {

                    LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
                    if(StrUtil.isNotBlank(req.getLoginName())){
                        queryWrapper.like(Employee::getLoginName, req.getLoginName());
                    }
                    //只想某几列(列少的时候可以这做)
                    queryWrapper.select(Employee::getId,Employee::getLoginName,
                            Employee::getAvatar,Employee::getStatus,Employee::getGender,
                            Employee::getCreateTime);
                    //想排序
                    queryWrapper.orderByAsc(Employee::getCreateTime);

                    //执行查询条件
                    employeeMapper.selectList(queryWrapper);

                });
    }

    @Override
    public boolean delete(Long[] ids) {
        Employee employee=new Employee();
        employee.setStatus("1");
        employee.setUpdateTime(new Date());
        employee.setUpdateBy(WaimaiThreadLocal.getName());

        List<Long> longs = Arrays.asList(ids);
        //超级管理员的ID=1不允许禁用的
        longs.remove(1L);

        LambdaQueryWrapper<Employee>  queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Employee::getId, ids);

        super.update(employee, queryWrapper);

        return true;
    }

    @Override
    public List<Employee> download(EmployeeReq req) {
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(req.getLoginName())){
            queryWrapper.like(Employee::getLoginName, req.getLoginName());
        }
        //只想某几列(列少的时候可以这做)
        queryWrapper.select(Employee::getId,Employee::getLoginName,
                Employee::getAvatar,Employee::getStatus,Employee::getGender,
                Employee::getCreateTime);
        //想排序
        queryWrapper.orderByAsc(Employee::getCreateTime);

        //执行查询条件
        List<Employee> employees = employeeMapper.selectList(queryWrapper);

        return employees;
    }

    @Override
    public AdminLoginResp login(AdminLoginReq req) {
        //支持 Lambda 形式调用：通过 Lambda 表达式，方便的编写各类查询条件，无需再担心字段写错
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getLoginName, req.getLoginName());
        Employee db = this.getOne(queryWrapper);
        if(db==null){
            throw new ServiceException(AckCode.USER_PASSWORD_NOT_FOUND);
        }
        //md5增强版 bcrypt --> Hutool工具类实现的 密码
        boolean correct= BCrypt.checkpw(req.getLoginPwd(), db.getLoginPwd());
        if(!correct){
            throw new ServiceException(AckCode.USER_PASSWORD_NOT_FOUND);
        }
        //状态
        if(db.getStatus().equals("1")){
            throw new ServiceException(AckCode.LOGIN_BANNED);
        }
        //转换自定义对象
        AdminLoginResp resp=new AdminLoginResp();
        resp.setId(db.getId());
        resp.setLoginName(db.getLoginName());
        resp.setName("后台管理员");
        resp.setAvatar(db.getAvatar());
        resp.setType(1);
        return resp;
    }
}




